Demographics

Row

Sex

Age

League

School

Sport

Sport Level

Row

RTL Summary

RTL Sex

RTL Age

RTL League

RTL School

RTL Sport

Row

RTP Summary

RTP Sex

RTP Age

RTP League

RTP School

RTP Sport

---
title: "HCAMP Paper Version 2"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source_code: embed
    vertical_layout: scroll
    theme: united
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(here)
library(janitor)
library(rio)
library(colorblindr)
library(gghighlight)
library(forcats)
library(ggrepel)
library(knitr)
library(kableExtra)
library(reactable)
library(plotly)
library(glue)
library(fs)
library(rstatix)
library(ggpubr)
library(writexl) 
library(remotes)
library(profvis) 


theme_fivethirtyeight <- function(base_size = 15, base_family = "") {
  theme_grey(base_size = base_size, base_family = base_family) %+replace%
    theme(
      
      # Base elements which are not used directly but inherited by others
      line =              element_line(colour = '#DADADA', size = 0.75, 
                                       linetype = 1, lineend = "butt"),
      rect =              element_rect(fill = "#F0F0F0", colour = "#F0F0F0", 
                                       size = 0.5, linetype = 1),
      text =              element_text(family = base_family, face = "plain",
                                       colour = "#656565", size = base_size,
                                       hjust = 0.5, vjust = 0.5, angle = 0, 
                                       lineheight = 0.9),
      
      # Modified inheritance structure of text element
      plot.title =        element_text(size = rel(1.5), family = '' , 
                                       face = 'bold', hjust = -0.05, 
                                       vjust = 1.5, colour = '#3B3B3B'),
      axis.title.x =      element_blank(),
      axis.title.y =      element_blank(),
      axis.text =         element_text(),
      
      # Modified inheritance structure of line element
      axis.ticks =        element_line(),
      panel.grid.major =  element_line(),
      panel.grid.minor =  element_blank(),
      
      # Modified inheritance structure of rect element
      plot.background =   element_rect(),
      panel.background =  element_rect(),
      legend.key =        element_rect(colour = '#DADADA'),
      
      # Modifiying legend.position
      legend.position = 'none',
      
      complete = TRUE
    )
}


theme_set(theme_fivethirtyeight())
```

```{r global, include=FALSE}
#all clean sims data
sims_concussion_data <- read_csv(here("data", "sims_concussion_data.csv"))

sims_concussion_data <- sims_concussion_data %>% 
  mutate(age = as.factor(age))

simsimp <- read_csv(here("data", "clean_impact_sims_data.csv"))

str(simsimp)

simsimp <- simsimp %>% 
  mutate(dataset = as.factor(dataset),
         school_year = as.factor(school_year),
         school = as.factor(school),
         league = as.factor(league),
         gender = as.factor(gender),
         age = as.factor(age),
         sport = as.factor(sport),
         injury = as.factor(injury))

```

```{r, include=FALSE}
#helpful functions 

mean_2 <- function(x) {
  z <- na.omit(x)
  sum(z) / length(z)
}

my_mean <- function(x) {
  mean(x[x >= 0], na.rm = TRUE)
}

create_react_time <- function(df, var) {
    df %>% 
      summarize(Mean = mean({{var}}),
                SD = sd({{var}}),
                Min = min({{var}}),
                Max = max({{var}}),
                Total = length({{var}})) %>% 
      mutate_if(is.numeric, round, 2) %>% 
      reactable(columns = list(
        Mean = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        SD = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        Min = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        Max = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " concussions"))
      ))
}

create_react_time2 <- function(df, x, var) {
    df %>% 
    group_by({{x}}) %>% 
      summarize(Mean = mean({{var}}),
                SD = sd({{var}}),
                Min = min({{var}}),
                Max = max({{var}}),
                Total = length({{var}})) %>% 
      mutate_if(is.numeric, round, 2) %>% 
      reactable(columns = list(
        Mean = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        SD = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        Min = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        Max = colDef(format = colFormat(separators = TRUE, suffix = " days")),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " concussions"))
      ))
}

create_react <- function(df, var) {
    df %>% 
      summarize(Mean = mean({{var}}),
                SD = sd({{var}}),
                Min = min({{var}}),
                Max = max({{var}}),
                Total = length({{var}})) %>% 
      mutate_if(is.numeric, round, 2) %>% 
      reactable(columns = list(
        Mean = colDef(format = colFormat(separators = TRUE)),
        SD = colDef(format = colFormat(separators = TRUE)),
        Min = colDef(format = colFormat(separators = TRUE)),
        Max = colDef(format = colFormat(separators = TRUE)),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " concussions"))
      ))
}


my_mean(simsimp$dys_btwn_onset_test_4)
```

```{r, include=FALSE}
simsimp %>% 
  count(student_id)

length(unique(simsimp$student_id))

simsimp %>% 
  group_by(row, gender) %>% 
  count()
```

# Demographics

Sidebar {.sidebar}
------------

The **Sex** table displays the total number of injuries by sex used in the data set. The total number of injuries is 755 that can be utilized for analysis. Like the previous iteration of the paper, some individuals sustained multiple injuries that are tracked individually. This is a characteristic that one of the reviewers specified we describe more to better explain the sample. The tables displayed present data representing the total number of _injuries_, which include instances of repeat injuries. Data on the number of unique individuals is outlined here: 

  *  **Number of females:** 271
  * **Number of males:** 460
  
  * 271 females sustained one tracked injury
  * 460 males sustained one tracked injury 
  * 10 females sustained two tracked injuries
  * 12 males sustained two tracked injuries
  * 1 female sustained three tracked injuries
  * 1 male sustained three tracked injuries 

Row {.tabset}
-----------------------------------------------------------------------

### Sex 

```{r, include=TRUE}
simsimp %>% 
  group_by(gender) %>% 
  summarize(total = n()) %>% 
  arrange(desc(total)) %>% 
  reactable(
    columns = list(
      gender = colDef(name = "Sex",
                      align = "center"),
      total = colDef(name = "Total",
                     align = "center",
                     format = colFormat(suffix = " injuries"))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
    )
```

```{r, include=FALSE}
sims_sex <- simsimp %>% 
  group_by(gender) %>% 
  summarize(total = n()) %>% 
  arrange(desc(total))

sims_sex_plot <- ggplot(sims_sex, aes(fct_reorder(gender, total), total)) +
  geom_col(fill = "blue",
           alpha = 0.7) +
  scale_y_continuous(limits = c(0, 600),
                     breaks = c(0, 200, 400, 600)) +
  coord_flip() +
  labs(x = "",
       y = "Total")
```

```{r, include=FALSE}
ggplotly(sims_sex_plot)
```

### Age

```{r, include=TRUE}
simsimp %>% 
  group_by(age) %>% 
  summarize(total = n()) %>% 
  reactable(
    columns = list(
      age = colDef(name = "Age",
                      align = "center"),
      total = colDef(name = "Total",
                     align = "center",
                     format = colFormat(suffix = " injuries"))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
    )
```

```{r, include=FALSE}
sims_age <- simsimp %>% 
  mutate(age = as.factor(age)) %>% 
  group_by(age) %>% 
  summarize(total = n()) %>% 
  arrange(desc(total))



sims_age_plot <- ggplot(sims_age, aes(fct_reorder(age, total), total)) +
  geom_col(fill = "blue",
           alpha = 0.7) +
  coord_flip() +
  labs(x = "Age",
       y = "Total")
```

```{r, include=FALSE}
ggplotly(sims_age_plot)
```

### League

```{r, include=TRUE}
simsimp %>% 
  group_by(league) %>% 
  summarize(total = n()) %>% 
  arrange(desc(total)) %>% 
  reactable(
    columns = list(
      league = colDef(name = "League",
                      align = "center"),
      total = colDef(name = "Total",
                     align = "center",
                     format = colFormat(suffix = " injuries"))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
    )
```

### School

```{r}
simsimp %>% 
  group_by(school) %>% 
  summarize(total = n()) %>% 
  arrange(desc(total)) %>% 
  reactable(
    columns = list(
      school = colDef(name = "School",
                      align = "center"),
      total = colDef(name = "Total",
                     align = "center",
                     format = colFormat(suffix = " injuries"))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE,
    searchable = TRUE
    )
```

### Sport

```{r}
simsimp %>% 
  group_by(sport) %>% 
  summarize(total = n()) %>% 
  arrange(desc(total)) %>% 
  reactable(
    columns = list(
      sport = colDef(name = "Sport",
                      align = "center"),
      total = colDef(name = "Total",
                     align = "center",
                     format = colFormat(suffix = " injuries"))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE,
    searchable = TRUE
    )
```

### Sport Level

```{r, include=FALSE}
simsimp %>% 
  group_by(level) %>% 
  summarize(total = n()) %>% 
  arrange(desc(total)) %>% 
  reactable(
    columns = list(
      level = colDef(name = "Level",
                      align = "center"),
      total = colDef(name = "Total",
                     align = "center",
                     format = colFormat(suffix = " injuries"))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
    )
```

Row {.tabset}
-----------------------------------------------------------------------

### RTL Summary 

```{r, include=TRUE}
create_react_time(simsimp, dys_btwn_onset_rtp_3)
```

### RTL Sex

```{r, include=TRUE}
create_react_time2(simsimp, gender, dys_btwn_onset_rtp_3)
```

### RTL Age

```{r, include=TRUE}
create_react_time2(simsimp, age, dys_btwn_onset_rtp_3)
```

### RTL League

```{r, include=TRUE}
create_react_time2(simsimp, league, dys_btwn_onset_rtp_3)
```

### RTL School

```{r, include=TRUE}
create_react_time2(simsimp, school, dys_btwn_onset_rtp_3)
```

### RTL Sport

```{r, include=TRUE}
create_react_time2(simsimp, sport, dys_btwn_onset_rtp_3)
```


Row {.tabset}
-----------------------------------------------------------------------

### RTP Summary 

```{r, include=TRUE}
create_react_time(simsimp, dys_btwn_onset_rtp_7)
```

### RTP Sex

```{r, include=TRUE}
create_react_time2(simsimp, gender, dys_btwn_onset_rtp_7)
```

### RTP Age

```{r, include=TRUE}
create_react_time2(simsimp, age, dys_btwn_onset_rtp_7)
```

### RTP League

```{r, include=TRUE}
create_react_time2(simsimp, league, dys_btwn_onset_rtp_7)
```

### RTP School

```{r, include=TRUE}
create_react_time2(simsimp, school, dys_btwn_onset_rtp_7)
```

### RTP Sport

```{r, include=TRUE}
create_react_time2(simsimp, sport, dys_btwn_onset_rtp_7)
```